import { useEffect } from 'react'
import style from './index.module.less'
import { Skeleton } from 'antd-mobile'
import { getSearch } from '@src/assets/js/tool'
import { useAppDispatch } from '@src/redux/hook'
import { setStructureName, setToken, setUserInfo } from '@src/redux/reducers/basic'
import { useHistory } from 'react-router-dom'
import { setList } from '@src/redux/reducers/pages'
import uuid from 'uuid'

function Main () {
  const dispatch = useAppDispatch()
  const h = useHistory()
  useEffect(() => {
    const o = getSearch()
    const u = typeof o.u === 'string' && o.u.length > 0 ? o.u : ''
    const t = typeof o.t === 'string' && o.t.length > 0 ? o.t : ''
    const p = typeof o.p === 'string' && o.p.length > 0 ? o.p : ''
    const n = typeof o.n === 'string' && o.n.length > 0 ? o.n : ''
    dispatch(setToken(t))
    if (n) {
      dispatch(setStructureName(n))
    }
    try {
      dispatch(setUserInfo(JSON.parse(u)))
    } catch (e) {
      console.error(e)
    }
    const l = [{ element: 'admin', title: '首页', id: uuid() }]
    if (p === 'register') {
      l.push({ element: 'addCaseProtocol', meta: { }, id: uuid() })
    }
    dispatch(setList(l))
    h.replace('/')
  }, [dispatch, h])
  return (
    <div className={style.container}>
      <Skeleton.Title animated />
      <Skeleton.Paragraph animated />
    </div>
  )
}

export default Main
